<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
<title>库存盘点</title>
<meta name="decorator" content="default"/>
<script src="${ctxStatic}/common/change/change.js"></script>
<style type="text/css">		
	#contentTable td ,#contentTable th{
		text-align: center;
	}
	.minWidth {
		min-width: 177px;
	}	
</style>
<script type="text/javascript">
$(document).ready(function() {
	initVaccName(bsVaccineBatchno.companyCode,bsVaccineBatchno.vaccineId);
	initVaccCompanyName(bsVaccineBatchno.companyCode);
	$(".vaccineCompany").change(function(){
		var vaccId = $(this).val();
		initCompanyName($(this).val());
		if(vaccId == ""){
			initVaccCompanyName("");
		}
	});
	
	$(document).on("blur",".sellprice",function(){
		var sellprice = parseFloat($(this).parent().parent().find("td[data-role=sellprice] input").val());
		var oldSellprice = parseFloat($(this).parent().attr("data-sellprice"));
		var text = "";
		if(oldSellprice > sellprice){
			text = (sellprice - oldSellprice) + "<span style='color:#ff0000'>(下调)</span>";
			$(this).addClass("alt");
		}else if(oldSellprice < sellprice){
			text = (sellprice - oldSellprice) + "<span style='color:#21C429'>(上调)</span>";
			$(this).addClass("alt");
		}else{
			$(this).removeClass("alt");
		}
		$(this).parent().parent().find("td[data-role=result]").html(text);
	});
	
	/* 保存 */
	$(document).on("click","#saveBtn",function(){
		var vailSuccess = false;
		var data = new Array();
		//循环取已修改的值
		$(".alt").each(function(){
			//组织提交数据
		    var sellprice = $(this).parent().parent().find("td[data-role=sellprice] input").val();
			var obj = new Object();
			obj.id=$(this).parent().attr("data-id");
			obj.sellprice = sellprice;
			data.push(obj);	
			vailSuccess = true;
		});
		//非空验证成功则提交保存
		if(vailSuccess){
			//提交
			//ajax提交后台
			$.ajax({
				url:"${ctx}/vaccinate/bsProduct/saveSellprice",
				type: "POST",
				data:{"list":JSON.stringify(data)},
				traditional: true, 
				success:function(data){
					if(data.code == 500){
						layer.msg(data.msg,{"icon":7});
					}else if(data.code == 200){
						layer.msg(data.msg, {"icon":1,"time":1500});
						$.each(JSON.parse(data.list), function(i,t){
							var str = $(".sellprice[data-id=" + t.id + "]");
							str.val(t.sellprice);
							str.parent().parent().find("td[data-role=sellprice]").html(t.sellprice);
							str.parent().parent().find("td[data-role=result]").html("");
						})	
						$("td[data-role=result]").html("");
						$(".sellprice").removeClass("alt");
					}	
				},
				error:function(a,b){
					console.error(a);
					layer.msg("保存失败",{"icon":2});
				}
			});
		}
	});
	
});
function page(n,s){
	$("#pageNo").val(n);
	$("#pageSize").val(s);
	$("#searchForm").submit();
    return false;
}
var bsVaccineBatchno = JSON.parse('${fns:toJson(bsProduct.bsVaccineBatchno)}');
var bsProduct = JSON.parse('${fns:toJson(bsProduct)}');
</script>
</head>
<body>
	<ul class="nav nav-tabs">
		<li><a href="${ctx}/vaccinate/bsProduct/check">库存盘点</a></li>
		<li class="active"><a href="${ctx}/vaccinate/bsProduct/modifyPrice">二类苗调价</a></li>
	</ul>
	<form:form id="searchForm" modelAttribute="bsProduct" action="${ctx}/vaccinate/bsProduct/modifyPrice" method="post" class="breadcrumb form-search ">
		<input id="pageNo" name="pageNo" type="hidden" value="${page.pageNo}"/>
		<input id="pageSize" name="pageSize" type="hidden" value="${page.pageSize}"/>
		<div class="breadcrumb">
			<table class="table table-bordered">
				<tr>
					<th>疫苗名称:</th>
					<td>
						<form:select path="bsVaccineBatchno.vaccineId" id="bsVaccineInfo-vaccineId" class="span2 minWidth vaccineCompany" >
							<form:option value="" label="--请选择--"/>
						</form:select>
					</td>
					<th>生产企业：</th>
					<td>
						<form:select path="bsVaccineBatchno.companyCode" id="companyCode" class="span2 minWidth changeCompany vaccCompanyName">
						    <form:option value="" label="--请选择--"/> 
						</form:select>
					</td>
				    <th>疫苗批号：</th>
					<td>
						<form:input path="bsVaccineBatchno.batchno" id="batchno" class="span2 minWidth"/>
					</td>
				</tr>
				<!-- <tr>
					<th>维护零库存：</th>
					<td colspan="5">
						<label class="" style="margin-right:13px;">
							<input id="check0" name="check0" type="checkbox" value="1"><i></i><span>维护0库存</span>
						</label>
					</td>
				</tr> -->
			</table>
		</div>
		<input id="btnSubmit" class="btn btn-primary" type="submit" value="查询"/>
	</form:form>
	
	<sys:message content="${message}"/>
	<table id="contentTable" class="table table-striped table-bordered table-condensed">
		<thead>
			<tr>
				<th>编号</th>
				<th>名称</th>
				<th>属性</th>
				<th>生产企业</th>
				<th>批号</th>
				<th>规格<br>(剂/支或粒)</th>
				<th>剂量</th>
				<th>剂型</th>
				<th>有效期</th>
				<th>成本价</th>
				<th>出售价</th>
				<th>价格调整</th>
				<th>实际库存</th>
			</tr>
		</thead>
		<tbody>
		<c:forEach items="${page.list}" var="bsProduct"  begin="0"  varStatus="status">
			<tr>
				<td>
					${(page.pageNo-1)*page.pageSize+status.index+1 }
				</td>
				<td>
					${bsProduct.bsVaccineBatchno.vaccineName}
				</td>
				<td>
					${fns:getDictLabel(bsProduct.cType,'bs_vaccine_type','') } 
				</td>
				<td>
					${bsProduct.bsVaccineBatchno.companyName}
				</td>
				<td>
					${bsProduct.bsVaccineBatchno.batchno}
					<c:if test="${not empty bsProduct.bsVaccineBatchno.secondBatchno}">
				        <span title="(查询批号时不需要带入亚批号)" style="color: #2fa4e7;">-${bsProduct.bsVaccineBatchno.secondBatchno}</span>
				    </c:if>
				</td>
				<td class="text-center">
					${bsProduct.bsVaccineBatchno.spec}
				</td>
				<td>
					${bsProduct.bsVaccineBatchno.dose}
				</td>
				<td>
					${bsProduct.bsVaccineBatchno.doseType}
				</td>
				<td>
					<fmt:formatDate value="${bsProduct.bsVaccineBatchno.outBoundDate}" pattern="yyyy-MM-dd"/>  
				</td>
				<td>
					￥${bsProduct.orignprice}
				</td>
				<td data-role ="sellprice" data-id="${bsProduct.id}" data-sellprice ="${bsProduct.sellprice}">
					￥<input type="text" value="${bsProduct.sellprice}" class="input-mini input-intable sellprice w45"/>
				</td>
				<td data-role="result">
					
				</td>
				<td data-role="storenum">
					${bsProduct.storenum}
				</td>
			</tr>
		</c:forEach>
		</tbody>
	</table>
	<div class="pagination">${page}</div>
	<div class="text-right">
		<button class="btn btn-primary" id="saveBtn">保存</button>
		<button class="btn btn-primary" onclick="history.go(-1)">关闭</button>
	</div>
</body>
</html>